Step 1: Import the requests library


In [1]:
import requests

Step 2: Get the file using the URL and assign it to a suitable variable. Thanks to Dr Chuck for the file and his awesome tutorials.


In [2]:
file = requests.get('http://data.pr4e.org/romeo.txt')

Step 3: Process the file according to your needs. Note that the contents of your file are stored in the "text" attribute.


In [3]:
# Put the lines and words in suitably-named lists. Note the use of strip() to remove any pesky extra lines or spaces.
linesList = file.text.strip().split('\n')
wordsList = file.text.strip().split()

# Print the file line-by-line.
for line in linesList:
    print (line)

# Print the number of words and lines.
print ()
print ('There are', len(wordsList), 'words and', len(linesList), 'lines in the file.')


But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief

There are 33 words and 4 lines in the file.